package zhu.areas.user.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import zhu.areas.user.data.User;
import zhu.areas.user.data.UserInfo;
import zhu.areas.user.service.IUserInfoService;
import zhu.areas.user.service.IUserService;

public class UserInfoAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private IUserInfoService userInfoService;
	private IUserService userService;
	private UserInfo userInfo=new UserInfo();
	private User user=new User();
	
	public String addUserInfo(){
		user=userService.findByUsername((String) ServletActionContext.getRequest().getSession().getAttribute("userName"));
		//System.out.println(user.getEmail());
		//System.out.println(userInfo.getRealName());
		userInfo.setUser(user);
		//userInfo.setUserID( (Integer) ServletActionContext.getRequest().getSession().getAttribute("userID"));
		if(userInfoService.addUserInfo(userInfo)){
			System.out.println("保存基本信息成功");
			return SUCCESS;
		}
		else{
			return ERROR;
		}
	
	}
	
	public String updateUserInfo(){

		//System.out.println(ServletActionContext.getRequest().getSession().getAttribute("userName"));
		user=userService.findByUsername((String) ServletActionContext.getRequest().getSession().getAttribute("userName"));
		//System.out.println(user.getEmail());
		//System.out.println(userInfo.getRealName());
		userInfo.setUser(user);
		userInfo.setUserID( (Integer) ServletActionContext.getRequest().getSession().getAttribute("userID"));
		if(userInfoService.updateUserInfo(userInfo)){
			System.out.println("修改基本信息成功");
			return SUCCESS;
		}
		else{
			return ERROR;
		}

	}
	public String showUserInfo(){
		System.out.println("here");
		System.out.println(ServletActionContext.getRequest().getSession().getAttribute("userID"));
		userInfo=userInfoService.getUserInfoByUserID((Integer) ServletActionContext.getRequest().getSession().getAttribute("userID"));
		//System.out.println(userInfo.getRealName());
		//System.out.println(userInfo.getBirthday());
		if(userInfo==null){
			System.out.println("空");
		}
		else{
			System.out.println("非空");
			ServletActionContext.getRequest().setAttribute("UserInfo",userInfo);
		}
		
		return SUCCESS;
		
	}
	

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public IUserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(IUserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

}
